package com.java.process.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.auth.service.SysUserService;
import com.java.common.utils.R;
import com.java.model.process.Process;
import com.java.process.service.ProcessService;
import com.java.vo.process.ApprovalVo;
import com.java.vo.process.ProcessFormVo;
import com.java.vo.process.ProcessQueryVo;
import com.java.vo.process.ProcessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/process")
@Api(tags = "审批流管理")
@CrossOrigin  //跨域
public class ProcessController {
    @Autowired
    private ProcessService processService;
    @Autowired
    private SysUserService sysUserService;

    @PreAuthorize("hasAuthority('bnt.process.list')")
    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public R index(@PathVariable Long page, @PathVariable Long limit, ProcessQueryVo processQueryVo){

        Page<ProcessVo> pageParam = new Page<>(page,limit);
        IPage<ProcessVo> newPage = processService.selectPage(pageParam,processQueryVo);
        return R.ok(newPage);
    }
    @CrossOrigin(origins = "*")
    @PostMapping ("/startUp")
    @ApiOperation("启动流程实例")
    public R startUp(@RequestBody ProcessFormVo processFormVo){
        processService.startUp(processFormVo);
        return R.ok();
    }
    @ApiOperation(value = "待处理")
    @GetMapping("/findPending/{page}/{limit}")
    public R findPending(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit
    ){
        Page<ProcessVo> pageParam = new Page<>(page,limit);
        return processService.findPending(pageParam);
    }
    @ApiOperation(value = "获取审批详情")
    @GetMapping("show/{id}")
    public R show(@PathVariable Long id){
        return processService.show(id);
    }
    @ApiOperation(value = "审批")
    @PostMapping("approve")
    public R approve(@RequestBody ApprovalVo approvalVo) {
        return processService.approve(approvalVo);
    }
    @ApiOperation(value = "已处理")
    @GetMapping("/findProcessed/{page}/{limit}")
    public R findProcessed(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<Process> pageParam = new Page<>(page, limit);
        return processService.findProcessed(pageParam);
    }
    @ApiOperation(value = "已发起")
    @GetMapping("/findStarted/{page}/{limit}")
    public R findStarted(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<ProcessVo> pageParam = new Page<>(page, limit);
        return processService.findStarted(pageParam);
    }
    @ApiOperation(value = "获取当前用户基本信息")
    @GetMapping("getCurrentUser")
    public R getCurrentUser() {
        return sysUserService.getCurrentUser();
    }
}
